<?php
namespace App\Services\Com;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithColumnLimit;

/**
 * Created by PhpStorm.
 * User: yzy
 * Date: 2019/6/26
 * Time: 19:57
 */
class ImportService implements ToCollection,WithColumnLimit
{
    public $data;
    protected $delTitle;
    protected $endColumn;

    /**
     *
     * @param $title integer   //去掉几行标题  默认一行
     */
    public function __construct($delTitle = 1, $endColumn='Z')
    {
        $this->delTitle = $delTitle;
        $this->endColumn = $endColumn;
    }

    /**
     * @param Collection $rows
     */
    public function collection(Collection $rows)
    {
        $this->delTitle($rows);

        $rows=json_decode(json_encode($rows),true);

        $data = [];

        foreach ($rows as $row){
            $tmp = array_filter($row, function ($num){
                return (is_null($num));
            });

            if(count($row) == count($tmp)) break;

            $data[] = $row;
        }
        $this->data = $data;
    }

    public function delTitle (&$rows) {
        $rows = $rows->slice($this->delTitle)->values();
    }

    public function endColumn(): string
    {
        //从A读取到L列,后面的列不再读取
        return $this->endColumn;
    }
}